package com.night.icm.service.base.service.comprehensive_test;

import com.night.icm.model.base.comprehensive_test.IdeologicalPointDTO;
import com.night.icm.persistence.base.entity.comprehensive_test.ComprehensiveScore;
import com.night.icm.persistence.base.entity.comprehensive_test.IdeologicalPoint;
import com.night.icm.persistence.base.repository.comprehensive_test.IdeologicalPointRepository;
import com.night.icm.persistence.business.entity.SystemFile;
import com.night.icm.persistence.business.reporsitory.SystemFileRepository;
import com.night.icm.service.base.mapper.comprehensive_test.IdeologicalPointMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
 * IdeologicalPointServiceImpl
 *
 * @author Liao ZhiYong
 * @date 2020/3/10
 */
@Service
@Transactional(readOnly = true, rollbackFor = Exception.class)
public class IdeologicalPointServiceImpl implements IdeologicalPointService {

    private final SystemFileRepository systemFileRepository;
    private final IdeologicalPointMapper ideologicalPointMapper;
    private final IdeologicalPointRepository ideologicalPointRepository;

    @Autowired
    public IdeologicalPointServiceImpl(SystemFileRepository systemFileRepository, IdeologicalPointMapper ideologicalPointMapper, IdeologicalPointRepository ideologicalPointRepository) {
        this.systemFileRepository = systemFileRepository;
        this.ideologicalPointMapper = ideologicalPointMapper;
        this.ideologicalPointRepository = ideologicalPointRepository;
    }

    /**
     * 新建思想分
     *
     * @param ideologicalPointDTO 思想分
     * @param comprehensiveScore  综合分
     * @return 思想分
     */
    @Override
    @Transactional(readOnly = true, rollbackFor = Exception.class)
    public IdeologicalPoint create(IdeologicalPointDTO ideologicalPointDTO, ComprehensiveScore comprehensiveScore) {
        IdeologicalPoint ideologicalPoint = this.ideologicalPointMapper.toEntity(ideologicalPointDTO);
        this.ideologicalPointRepository.save(ideologicalPoint);
        // 保存文体分的附件，并关联文体分附件
        List<String> fileIds = ideologicalPointDTO.getFileIds();
        saveFile(ideologicalPoint, fileIds);
        return ideologicalPoint;
    }

    /**
     * 更新思想分
     *
     * @param ideologicalPointDTO 思想分
     * @param comprehensiveScore  综合分
     * @return 思想分
     */
    @Override
    @Transactional(readOnly = true, rollbackFor = Exception.class)
    public IdeologicalPoint update(IdeologicalPointDTO ideologicalPointDTO, ComprehensiveScore comprehensiveScore) {
        // 更新思想分
        Optional<IdeologicalPoint> byId = this.ideologicalPointRepository.findById(comprehensiveScore.getIdeologicalPoint().getId());
        IdeologicalPoint ideologicalPoint = byId.get();
        this.ideologicalPointMapper.updateEntity(ideologicalPointDTO, ideologicalPoint);
        // 删除原有附件
        ideologicalPoint.setFiles(null);
        this.ideologicalPointRepository.save(ideologicalPoint);
        // 添加新的附件
        List<String> fileIds = ideologicalPointDTO.getFileIds();
        saveFile(ideologicalPoint, fileIds);
        this.ideologicalPointRepository.save(ideologicalPoint);
        return ideologicalPoint;
    }


    /**
     * 保存文件
     *
     * @param ideologicalPoint 对象
     * @param fileIds          文件id数组
     */
    private void saveFile(IdeologicalPoint ideologicalPoint, List<String> fileIds) {
        if (null != fileIds && !fileIds.isEmpty()) {
            List<Long> ids = new ArrayList<>();
            fileIds.forEach(id -> ids.add(Long.parseLong(id)));
            List<SystemFile> files = this.systemFileRepository.findAllById(ids);
            ideologicalPoint.setFiles(files);
        }
    }
}